//------------------------------------------------------------------------------
// <copyright file="WebDataService.svc.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Data.Services.Common;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;

namespace SensorWebApplication
{
    public class SensorDataService : DataService<SensorDbEntities>
    {
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(DataServiceConfiguration config)
        {
            // Set rules to indicate which entity sets and service operations are visible, updatable, etc.
            config.SetEntitySetAccessRule("TemperatureTable", EntitySetRights.All);
            config.SetServiceOperationAccessRule("UploadTemp", ServiceOperationRights.AllRead);
            config.SetServiceOperationAccessRule("QueryLastTemp", ServiceOperationRights.AllRead);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
        }

        #region AzureSensor Specific

        // This method is for SensorApp to upload temperature data
        [WebGet]
        public IQueryable<TemperatureTable> UploadTemp(Int32 temp)
        {
            SensorDbEntities context = this.CurrentDataSource;

            if (null == context)
            {
                throw new ArgumentNullException("dataSource",
                    "dataSource is Null");
            }
            try
            {
                // Save data to DB
                TemperatureTable newData = new TemperatureTable();
                newData.Temperature = temp;
                context.TemperatureTable.Add(newData);
                context.SaveChanges();

                // Retrieve the last added data from DB
                var selectedData = from data in context.TemperatureTable
                                   where data.ID == (from x in context.TemperatureTable select (x.ID)).Max()
                                   select data;

                return selectedData;
            }
            catch (Exception ex)
            {
                throw new ApplicationException(string.Format(
                    "An error occurred: {0}", ex.Message));
            }

        }

        // This method is for MonitorApp to query last temperature
        [WebGet]
        public IQueryable<TemperatureTable> QueryLastTemp()
        {
            SensorDbEntities context = this.CurrentDataSource;

            if (null == context)
            {
                throw new ArgumentNullException("dataSource",
                    "dataSource is Null");
            }
            try
            {
                // Retrieve the last added data from DB
                var selectedData = from data in context.TemperatureTable
                                   where data.ID == (from x in context.TemperatureTable select (x.ID)).Max()
                                   select data;

                return selectedData;
            }
            catch (Exception ex)
            {
                throw new ApplicationException(string.Format(
                    "An error occurred: {0}", ex.Message));
            }

        }

        #endregion

    }
}
